package com.weng.platform.apiManager.doc.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.apiManager.doc.entity.DocLock;
import com.weng.platform.apiManager.doc.model.vo.DocLockVo;
import com.weng.platform.apiManager.doc.service.IDocLockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;


/**
 * 文档编辑锁 API实现
 *
 * @author wengzhonghui
 * @date 2020-09-25 18:40:12
 *
 */  
@Slf4j
@RestController
@RequestMapping("/doc-lock")
@Api(value = "文档编辑锁api", tags = {"文档编辑锁操作接口"})
public class DocLockApi extends BaseController {
    @Autowired
    private IDocLockService docLockService;

    /**
     * 功能描述:单个获取文档编辑锁
     */
    @ApiOperation(value = "获取文档编辑锁", notes = "")
    @GetMapping(value = "/get")
    public ResponseData get(String id){
        DocLock docLock = docLockService.getById(id);
        return success(docLock);
    }


    /**
     * 条件 分页查询文档编辑锁
     */
    @ApiOperation(value = "分页获取文档编辑锁", notes = "")
    @GetMapping(value = "/list")
    public ResponseData list(Integer pageNo,Integer pageSize){
		IPage<DocLock> iPage = docLockService.list(pageNo, pageSize);
        return success(iPage);
    }


    /**
	 * 新增文档编辑锁
	 */
	@ApiOperation(value = "新增文档编辑锁", notes = "")
	@PostMapping(value = "/add")
	public ResponseData add(@Valid @RequestBody DocLockVo docLockReq){
		DocLock docLock = Convert.convert(DocLock.class, docLockReq);
		docLockService.add(docLock);
		return success();
	}

	/**
	 * 更新文档编辑锁
	 */
	@ApiOperation(value = "更新文档编辑锁", notes = "")
	@PostMapping(value = "/update")
	public ResponseData update(@Valid @RequestBody DocLockVo docLockReq){
		DocLock docLock = Convert.convert(DocLock.class, docLockReq);
		docLockService.update(docLock);
		return success();
	}


	/**
	 * 删除文档编辑锁
	 */
	@ApiOperation(value = "ids 删除文档编辑锁", notes = "可以删除多个，中间以,号分隔")
	@PostMapping(value = "/delete")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		boolean flag = docLockService.removeByIds(Arrays.asList(ids.split(",")));
		if(flag){
			return success(deleteReq.getIds());
		}else{
			return failed("删除失败");
		}
	}
}
